package hot100;

import java.util.HashMap;
import java.util.Map;

public class Solution128 {
    public int longestConsecutive(int[] nums) {
        HashMap<Integer, Integer> map = new HashMap<>();
        int res = 0;
        for (int x : nums) {
            if (!map.containsKey(x)) {
                int left = map.getOrDefault(x - 1, 0);
                int right = map.getOrDefault(x + 1, 0);
                int cur = left + right + 1;
                res = Math.max(res, cur);
                map.put(x, cur);
                map.put(x - left, cur);
                map.put(x + right, cur);
            }
        }
        return res;
    }
}
